<?php

	/**
	 * Defines a class to be used for Varchar Data Typing with PHP
	 * 
	 * @author Pablo Santiago Sánchez <phackwer@gmail.com>
	 * @copyright Copyright (c) 2008, Pablo Santiago Sánchez
	 * @license http://opensource.org/licenses/bsd-license.php BSD License
	 * @package pop
	 * @subpackage datatypes
	 */

	/**
	 * Class used to handle varchar values.
	 * Only n chars allowed.
	 * 
	 * @package pop
	 * @subpackage datatypes
	 */
	class PVarchar extends PTypeBase
	{
		protected	$size;

		/**
		 * Constructor, in which you must inform the default value and size.
		 * If none is informed, it uses empty as default and 255 as max char size
		 * @param string $defaultval Default value.
		 * @param integer $size maximum size.
		 */
		public function __construct($defaultval = "", $size = 255)
		{
			$this->size 		= $size;
			$this->__set("value", $defaultval);
			$this->defaultval 	= $defaultval;
		}

		/**
		 * Magic method used to set the value and check if it's valid
		 * @param string $name name of the value
		 * @param string $value value itself
		 * @return bool 
		 */
		public function __set($name, $value)
		{
			if (strlen($value) > $this->size)
			{
				echo strlen($value);
				echo $this->size;
				throw new Exception("This value is too long.\nValue: <font color=#FF0000>".$value."</font>\nMaxsize: <font color=#FF0000>".$this->size."</font>");
			}
			else if ($name != "value")
			{
				throw new Exception("This attribute cannot be set");
			}
			else
			{
				$this->$name = $value;
				return true;
			}
		}
	}

?>
